package MyFragment;

import android.app.usage.NetworkStats;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.TrafficStats;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.qb.devtest.MainActivity;
import com.qb.devtest.MyApplication;
import com.qb.devtest.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import utils.AppUtil;
import utils.DevInfoUtil;
import utils.MyUtil;
import utils.NetWorkListenerUtils;

public class FragmentNetwork extends Fragment {
    public TextView tvNetType1,tvNetType2,tvNetType3,tvNetMac1,tvNetMac2,tvNetMac3;
    public TextView tvLocal1,tvLocal1Ipv6,tvLocal1Ipv4;
    public TextView tvLocal2,tvLocal2Ipv6,tvLocal2Ipv4;
    public TextView tvLocal3,tvLocal3Ipv6,tvLocal3Ipv4;
    public TextView tvNetSpeed,tvTrafficTotal,tvTrafficMobile,tvTrafficWifi,tvTrafficApps;
    Context ctx = MyApplication.getInstance();
    SharedPreferences SP = ctx.getSharedPreferences("dev_test_setting", ctx.MODE_PRIVATE);
    NetWorkListenerUtils nwl;
    private Handler mHnadler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    tvNetSpeed.setText(" 当前网速  " + msg.obj.toString());
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_network, container, false);
        tvNetType1 = view.findViewById(R.id.tv_net_type1);
        tvNetType2 = view.findViewById(R.id.tv_net_type2);
        tvNetType3 = view.findViewById(R.id.tv_net_type3);
        tvNetMac1 = view.findViewById(R.id.tv_net_mac1);
        tvNetMac2 = view.findViewById(R.id.tv_net_mac2);
        tvNetMac3 = view.findViewById(R.id.tv_net_mac3);
        tvLocal1 = view.findViewById(R.id.tv_local1);
        tvLocal1Ipv6 = view.findViewById(R.id.tv_local1_ipv6);
        tvLocal1Ipv4 = view.findViewById(R.id.tv_local1_ipv4);
        tvLocal2 = view.findViewById(R.id.tv_local2);
        tvLocal2Ipv6 = view.findViewById(R.id.tv_local2_ipv6);
        tvLocal2Ipv4 = view.findViewById(R.id.tv_local2_ipv4);
        tvLocal3 = view.findViewById(R.id.tv_local3);
        tvLocal3Ipv6 = view.findViewById(R.id.tv_local3_ipv6);
        tvLocal3Ipv4 = view.findViewById(R.id.tv_local3_ipv4);

        tvNetSpeed = view.findViewById(R.id.tv_net_speed);
        nwl = new NetWorkListenerUtils(ctx,mHnadler);
        tvTrafficTotal = view.findViewById(R.id.tv_traffic_total);
        tvTrafficMobile = view.findViewById(R.id.tv_traffic_mobile);
        tvTrafficWifi = view.findViewById(R.id.tv_traffic_wifi);
        tvTrafficApps = view.findViewById(R.id.tv_traffic_apps);

        Button btnCopy = view.findViewById(R.id.btn_copy);
        btnCopy.setOnClickListener(view1 -> {
            String s = tvNetType1.getText()+"\n"+tvNetType2.getText()+"\n"+tvNetType3.getText()+"\n"
                    +tvNetMac1.getText()+"\n"+tvNetMac2.getText()+"\n"+tvNetMac3.getText()+"\n"
                    +tvLocal1.getText()+"\n"+tvLocal1Ipv6.getText()+"\n"+tvLocal1Ipv4.getText()+"\n"
                    +tvLocal2.getText()+"\n"+tvLocal2Ipv6.getText()+"\n"+tvLocal2Ipv4.getText()+"\n"
                    +tvLocal3.getText()+"\n"+tvLocal3Ipv6.getText()+"\n"+tvLocal3Ipv4.getText()+"\n"
                    +tvNetSpeed.getText()+"\n"+tvTrafficTotal.getText()+"\n"+tvTrafficMobile.getText()+"\n"
                    +tvTrafficWifi.getText()+"\n"+tvTrafficApps.getText();
            MyUtil.copyStr(ctx, s);
        });

        loadData();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        nwl.unbindShowNetSpeed();
    }

    public void loadData() {
        JSONArray networkType = DevInfoUtil.getNetworkType(ctx);
        String type = "unKnow";
        String mac = "unKnow";
        try {
            JSONObject o1 = networkType.getJSONObject(0);
            type = o1.getString("type");
            mac = o1.getString("mac");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tvNetType1.setText(" 网络类型  "+type);
        tvNetMac1.setText(" MAC地址  "+mac);
        try {
            JSONObject o2 = networkType.getJSONObject(1);
            tvNetType2.setText(" 网络类型  "+o2.getString("type"));
            tvNetMac2.setText(" MAC地址  "+o2.getString("mac"));
        } catch (JSONException e) {
            tvNetType2.setVisibility(View.GONE);
            tvNetMac2.setVisibility(View.GONE);
//            e.printStackTrace();
        }
        try {
            JSONObject o3 = networkType.getJSONObject(2);
            tvNetType3.setText(" 网络类型  "+o3.getString("type"));
            tvNetMac3.setText(" MAC地址  "+o3.getString("mac"));
        } catch (JSONException e) {
            tvNetType3.setVisibility(View.GONE);
            tvNetMac3.setVisibility(View.GONE);
//            e.printStackTrace();
        }

        JSONArray localIP = DevInfoUtil.getLocalIP();
        try {
            JSONObject o1 = localIP.getJSONObject(0);
            tvLocal1.setText(" 本地网络  "+o1.getString("type"));
            tvLocal1Ipv6.setText(" IPV6  "+o1.getString("ipv6"));
            tvLocal1Ipv4.setText(" IPV4  "+o1.getString("ipv4"));
        } catch (JSONException e) {
            tvLocal1.setVisibility(View.GONE);
            tvLocal1Ipv6.setVisibility(View.GONE);
            tvLocal1Ipv4.setVisibility(View.GONE);
//            e.printStackTrace();
        }
        try {
            JSONObject o2 = localIP.getJSONObject(1);
            tvLocal2.setText(" 本地网络  "+o2.getString("type"));
            tvLocal2Ipv6.setText(" IPV6  "+o2.getString("ipv6"));
            tvLocal2Ipv4.setText(" IPV4  "+o2.getString("ipv4"));
        } catch (JSONException e) {
            tvLocal2.setVisibility(View.GONE);
            tvLocal2Ipv6.setVisibility(View.GONE);
            tvLocal2Ipv4.setVisibility(View.GONE);
//            e.printStackTrace();
        }
        try {
            JSONObject o3 = localIP.getJSONObject(2);
            tvLocal3.setText(" 本地网络  "+o3.getString("type"));
            tvLocal3Ipv6.setText(" IPV6  "+o3.getString("ipv6"));
            tvLocal3Ipv4.setText(" IPV4  "+o3.getString("ipv4"));
        } catch (JSONException e) {
            tvLocal3.setVisibility(View.GONE);
            tvLocal3Ipv6.setVisibility(View.GONE);
            tvLocal3Ipv4.setVisibility(View.GONE);
//            e.printStackTrace();
        }

        tvNetSpeed.setText(" 当前网速  0 kb/s");
        nwl.startShowNetSpeed();
        JSONObject trafficTotal = MyUtil.getTrafficTotal(ctx,0);
        long mRx = 0;
        long mTx = 0;
        long wRx = 0;
        long wTx = 0;
        try {
            mRx = trafficTotal.getLong("mRx");
            mTx = trafficTotal.getLong("mTx");
            wRx = trafficTotal.getLong("wRx");
            wTx = trafficTotal.getLong("wTx");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        tvTrafficMobile.setText(" 已用移动流量  "+mRx/1024/1024+"MB（下行）+  "+mTx/1024/1024+"MB（上行）=  "+(mRx+mTx)/1024/1024+"MB（合计）");
        tvTrafficWifi.setText(" 已用WIFI流量  "+wRx/1024/1024+"MB（下行）+  "+wTx/1024/1024+"MB（上行）=  "+(wRx+wTx)/1024/1024+"MB（合计）");
        tvTrafficMobile.setVisibility(View.GONE);
        tvTrafficWifi.setVisibility(View.GONE);
//        tvTrafficTotal.setText(" 已使用流量  "+(mRx+wRx)/1024/1024+"MB（下行）+  "+(mTx+wTx)/1024/1024+"MB（上行）=  "+(mRx+mTx+wRx+wTx)/1024/1024+"MB（合计)");
//        tvTrafficTotal.setText(" 已使用流量  "+(mRx+mTx)/1024/1024+"MB（移动）+  "+(wTx+wRx)/1024/1024+"MB（WIFI）=  "+(mRx+mTx+wRx+wTx)/1024/1024+"MB（合计)");
        long mTrafficTotal=0;
        long wTrafficTotal=0;
        JSONArray appList = AppUtil.getAppList(ctx);
        String trafficApps = "";
        for (int i=0;i<appList.length();i++) {
            String appName = "";
            mRx=0;mTx=0;wRx=0;wTx=0;
            try {
                JSONObject o = appList.getJSONObject(i);
                appName = o.getString("name");
                JSONObject to = o.getJSONObject("traffic");
                mRx = to.getLong("mRx");
                mTx = to.getLong("mTx");
                wRx = to.getLong("wRx");
                wTx = to.getLong("wTx");

            } catch (JSONException e) {
                e.printStackTrace();
            }
            long mTotal = mRx+mTx;
            long wTotal = wRx+wTx;
            long tTotal = mRx+mTx+wRx+wTx;
            String mTotalStr = mTotal+"byte";
            if (mTotal/1024<=1024) {
                mTotalStr = mTotal/1024+"kb";
            } else {
                mTotalStr = mTotal/1024/1024+"MB";
            }
            String wTotalStr = wTotal+"byte";
            if (wTotal/1024<=1024) {
                wTotalStr = wTotal/1024+"kb";
            } else {
                wTotalStr = wTotal/1024/1024+"MB";
            }
            String tTotalStr = tTotal+"byte";
            if (tTotal/1024<=1024) {
                tTotalStr = tTotal/1024+"kb";
            } else {
                tTotalStr = tTotal/1024/1024+"MB";
            }
            trafficApps = trafficApps+appName+"  "+mTotalStr+"（移动）+  "+wTotalStr+"（WIFI）=  "+tTotalStr+"（合计）"+"\n\n";
            mTrafficTotal = mTrafficTotal + mTotal;
            wTrafficTotal = wTrafficTotal + wTotal;
        }
        tvTrafficApps.setText(trafficApps);
        String mTrafficTotalStr = mTrafficTotal+"byte";
        if (mTrafficTotal/1024<=1024) {
            mTrafficTotalStr = mTrafficTotal/1024+"kb";
        } else {
            mTrafficTotalStr = mTrafficTotal/1024/1024+"MB";
        }
        String wTrafficTotalStr = wTrafficTotal+"byte";
        if (wTrafficTotal/1024<=1024) {
            wTrafficTotalStr = wTrafficTotal/1024+"kb";
        } else {
            wTrafficTotalStr = wTrafficTotal/1024/1024+"MB";
        }
        long tTrafficTotal = mTrafficTotal+wTrafficTotal;
        String tTrafficTotalStr = tTrafficTotal+"byte";
        if (tTrafficTotal/1024<=1024) {
            tTrafficTotalStr = tTrafficTotal/1024+"kb";
        } else {
            tTrafficTotalStr = tTrafficTotal/1024/1024+"MB";
        }
        tvTrafficTotal.setText(" 已使用流量  "+mTrafficTotalStr+"（移动）+  "+wTrafficTotalStr+"（WIFI）=  "+tTrafficTotalStr+"（合计)");
    }


}